package com.pineapple.module.web.graph;

import java.sql.Connection;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.pineapple.module.core.graph.domain.GraphModel;
import com.pineapple.module.core.graph.service.GraphManager;
import com.pineapple.module.util.connection.DBCPConnection;

public class GraphAction extends ActionSupport implements ModelDriven<GraphModel> {
	private static final long serialVersionUID = -3661000161146714152L;

	private GraphModel model = new GraphModel();

	public String init() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute("user") == null) {
			return "init";
		}
		GraphManager manager = new GraphManager();
		try (Connection conn = new DBCPConnection().getConnection()) {
			model.setLstSearch(manager.search(model.getCriteria(), conn));
		} catch (Exception ex) {
			ex.printStackTrace();

		}

		return "search";
	}

	@Override
	public GraphModel getModel() {
		return model;
	}
}
